package com.guangzhi.weijianzhi.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.guangzhi.weijianzhi.context.WeiJianZhiEntity;
import com.guangzhi.weijianzhi.qiniu.JSONObjectRet;
import com.guangzhi.weijianzhi.qiniu.PutExtra;
import com.guangzhi.weijianzhi.qiniu.ResumableIO;
import com.guangzhi.weijianzhi.utils.Misc;

/**
 * UpImageService服务
 * 
 * @author <a href="http://www.xunhou.me" target="_blank">Kelvin</a>
 *
 */
public class UpImageService extends Service{
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		if(intent!=null){
			
		
		
		ArrayList<String> applyList = intent.getStringArrayListExtra("applylist");
		ArrayList<String> nameList = intent.getStringArrayListExtra("namelist");
		PutExtra e = new PutExtra();
		e.notify = new PutExtra.INotify() {
			@Override
			public void onSuccessUpload(PutExtra ex) {
				if (ex.isFinishAll()) return;
				JSONObject json;
				try {
					json = ex.toJSON();
				} catch (JSONException e1) {
					e1.printStackTrace();
					return;
				}
			  }
			};
			
		doResumableUpload(true,applyList,nameList, e);
		}
		return super.onStartCommand(intent, flags, startId);
	
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	public void doResumableUpload(Boolean flag , final List<String> applyList, List<String> nameList, PutExtra extra) {
//		 final NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//		 final Notification  notification=new Notification(R.drawable.applogo, "文件正在上传，请勿关闭APP。", System.currentTimeMillis());
//		 notification.flags=Notification.FLAG_AUTO_CANCEL;
//		 notification.vibrate=new long[]{1000,200,1000};
//		 Intent intent=new Intent();
//		 PendingIntent contentIntent=PendingIntent.getActivity(this, 0, intent , 0);
//		 notification.setLatestEventInfo(this, "文件正在上传中~", "上传中，请勿关闭APP。",contentIntent);	
//		 final Notification  notification1=new Notification(R.drawable.applogo, "上传成功~", System.currentTimeMillis());
//		 notification1.flags=Notification.FLAG_AUTO_CANCEL;//notification显示完自动清空
//		 notification1.vibrate=new long[]{1000,200,1000};
//		 Intent intent1=new Intent();
//		 PendingIntent contentIntent1=PendingIntent.getActivity(this, 0, intent1 , 0);
//		 notification1.setLatestEventInfo(this, "上传成功~", "上传成功~",contentIntent1);	
		 for (int i = 0; i < applyList.size();i++) {
				String key = nameList.get(i);
				Log.d("======七牛name=", key);
				String token = WeiJianZhiEntity.qiNiuiToken;
				extra.params = new HashMap<String, String>();
				extra.params.put("x:a", "bb");
				ResumableIO.putFile(this, token, key, applyList.get(i), extra, new JSONObjectRet() {
					@Override
					public void onSuccess(JSONObject obj) {
						Misc.log("ok===ok");
						System.out.println("SUCCESS");
					}

					@Override
					public void onProcess(long current, long total) {
						float percent = (float) (current*10000/total) / 100;
						Misc.log("uping===uping");
					}

					@Override
					public void onPause(Object tag) {
						Misc.log("pause===pause");
						System.out.println("PAUSE");
					}

					@Override
					public void onFailure(Exception ex) {
						Misc.log("false===false");
						System.out.println("FAIL");
					}
				});
			}
	}

}


